//
//  LineBelowOrderVc.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/19.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "LineBelowOrderVc.h"
#import "LineBelowOrderCell.h"

#import "config.h"
#import "StringTool.h"
#import "MBProgressHUD+MJ.h"
#import "NSString+category.h"
#import "BrokerDataMange.h"
#import "OrderApplayModel.h"

#import "UserModal.h"
#import "config.h"
#import "NSString+category.h"


@interface LineBelowOrderVc ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UITextViewDelegate>

@property (strong, nonatomic) LineBelowOrderCell *cell;
@property (strong, nonatomic) BrokerDataMange *dataMange;
@property (strong, nonatomic) OrderApplayModel *model;
@end

@implementation LineBelowOrderVc
@synthesize dataMange,model,cell;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
   
}


-(void)initView
{
    
     [self.titleView setMianTitle:@"线下下单申请"];
    [[self.titleView doneBtn] setImage:nil forState:UIControlStateNormal];
    [[self.titleView doneBtn] setTitle:@"提交" forState:UIControlStateNormal];
    [[self.titleView doneBtn].titleLabel setFont:[UIFont systemFontOfSize:15]];
    [self.titleView.doneBtn setAttributedTitle: [NSString makeLineStyle:@"提交" color:[UIColor whiteColor] fontSize:14] forState:UIControlStateNormal];
    [[self.titleView doneBtn]setTag:100];
    [[self.titleView doneBtn]addTarget:self action:@selector(btnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    dataMange = [[BrokerDataMange alloc]init];

}

#pragma mark 点击保存
-(void)btnOnclick:(UIButton *)btn

{
    if ([self checkInfo]) {
    
        [MBProgressHUD showMessage:@"请稍等" toView:self.view];
        
        __weak typeof(self)bself = self;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            
            ResulteMod *mod = [dataMange orderapplyWithOrderModel:model];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [MBProgressHUD hideHUDForView:bself.view animated:YES];
                [bself.navigationController popViewControllerAnimated:YES];
                if (mod.resltState == ResultFail) {
                    [MBProgressHUD showError:mod.resultTip];
                }else if (mod.resltState == ResultNetError)
                {
                    [MBProgressHUD showError:@"网络错误!"];
                }
                else
                {
    
                    [MBProgressHUD showSuccess:@"提交成功"];
                }
                
               
                
            });
        });
        
    }
}

#pragma mark 判断输入不空
-(BOOL)checkInfo
{
    if ([StringTool isBlankString:cell.nametf.text]) {
        [MBProgressHUD showError:@"请输入用户名!"];
        return NO;
    }
    if ([StringTool isBlankString:cell.phonetf.text]) {
        [MBProgressHUD showError:@"请输入手机号!"];
        return NO;
    }else
    {
        if (![self validateMobile:cell.phonetf.text]) {
            [MBProgressHUD showError:@"请输入正确的手机号!"];
            return NO;
        }
    }
    
    if ([StringTool isBlankString:cell.idCardtf.text]) {
        [MBProgressHUD showError:@"请输入身份证号!"];
        return NO;
    }else
    {
        if (![NSString validateIdentityCard:cell.idCardtf.text]) {
            [MBProgressHUD showError:@"请输入正确的身份证号!"];
            return NO;
        }
    }
    
    if ([StringTool isBlankString:cell.housingNOtf.text]) {
        [MBProgressHUD showError:@"请输入房型编号ID!"];
        return NO;
    }
    
    if ([StringTool isBlankString:cell.premisesNotf.text]) {
        [MBProgressHUD showError:@"请输入楼盘编号ID!"];
        return NO;
    }
    if ([StringTool isBlankString:cell.banktf.text]) {
        [MBProgressHUD showError:@"请输入开户行"];
        return  NO;
    }
    
    if ([StringTool isBlankString:cell.banktf.text]) {
        [MBProgressHUD showError:@"请选择开户行!"];
        return NO;
    }
    if ([StringTool isBlankString:cell.bankCardNametf.text]) {
        [MBProgressHUD showError:@"请输入开户名!"];
        return NO;
    }
    if ([StringTool isBlankString:cell.bankCardIdtf.text]) {
        [MBProgressHUD showError:@"请输入银行卡号!"];
        return NO;
    }
    if ([StringTool isBlankString:cell.amouttf.text]) {
        [MBProgressHUD showError:@"请输入汇款金额!"];
        return NO;
    }

    model = [[OrderApplayModel alloc]init];
    
    model.csmName = cell.nametf.text;
    model.csmPhone = cell.phonetf.text;
    model.csmIdCard = cell.idCardtf.text;
    model.housingBusinessNo = cell.housingNOtf.text;
    model.premisesBusinessNo = cell.premisesNotf.text;
    
    model.bank = cell.banktf.text;
    model.bankCardName = cell.bankCardNametf.text;
    model.bankCardId = cell.bankCardIdtf.text;
    model.amount = cell.amouttf.text;
    
    model.remarks = cell.remarktf.text;
    
    return YES;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 845;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   cell = [tableView dequeueReusableCellWithIdentifier:@"lineBelowOrderCell"];
    
    if (!cell) {
         cell = [[[NSBundle mainBundle]loadNibNamed:@"LineBelowOrderCell" owner:nil options:nil]lastObject];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.remarktf.delegate = self;
        cell.amouttf.delegate = self;
        cell.bankCardIdtf.delegate = self;
        cell.bankCardNametf.delegate = self;
        cell.banktf.delegate = self;
        
        cell.premisesNotf.delegate = self;
        cell.housingNOtf.delegate = self;
        
        cell.idCardtf.delegate = self;
        cell.phonetf.delegate = self;
        cell.nametf.delegate = self;
        
        
    }
    
    return cell;
}

- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
    
    CGRect dd =  [textField.superview convertRect:textField.frame toView:self.cell];
    
    CGFloat tvbotY = dd.size.height+dd.origin.y;
    int mid  = PMH-64 -[UserModal shareUserModel].keyboardHeight-tvbotY;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.cell.frame;
        buf.origin.y = up ? mid : 0;
        [self.cell setFrame:buf];
        
        [UIView commitAnimations];
    }else if(up==false){
        
        [self viewRest];
    }
}



-(void)viewRest{
    
    
    const float movementDuration = 0.3f;
    [UIView beginAnimations: nil context: nil];
    [UIView setAnimationBeginsFromCurrentState: YES];
    [UIView setAnimationDuration: movementDuration];
    
    CGRect buf = self.cell.frame;
    buf.origin.y =  0;
    [self.cell setFrame:buf];
    
    [UIView commitAnimations];
}

#pragma et delegare
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self animateTextField: textField up: YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    [self animateTextField:textField up:false];
    
    [textField resignFirstResponder];
    
    [self viewRest];
    return  YES;
}

#pragma mark textView的代理
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView;
{
    
    int mid  = PMH-[UserModal shareUserModel].keyboardHeight-760;
    
    if (mid<0) {
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.cell.frame;
        buf.origin.y = mid;
        [self.cell setFrame:buf];
        
        [UIView commitAnimations];
    }
    return YES;
}
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text
{
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        
        const float movementDuration = 0.3f;
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        
        CGRect buf = self.cell.frame;
        buf.origin.y =  0;
        [self.cell setFrame:buf];
        
        [UIView commitAnimations];
        
        return NO;
    }

    return YES;
}


@end
